﻿//----------------------------------------------------
//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Data;
using System.Text;
using System.Collections.Generic;

using System.Linq;

using WinnerCMS.Common;
using WinnerCMS.Model;
using System.Data.Entity;

namespace WinnerCMS.Data
{
    /// <summary>
    /// 数据访问层CMS_DeliveryMethod。
    /// </summary>
    public class DeliveryMethodDal
    {
        #region  添加数据
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="model"></param>
        public void Add(DeliveryMethod model)
        {
            using (Entities db = new Entities())
            {
                db.DeliveryMethods.Add(model);
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region  修改数据
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="model"></param>
        public void Edit(DeliveryMethod model)
        {
            using (Entities db = new Entities())
            {
                db.DeliveryMethods.Attach(model);
                db.Entry(model).State = EntityState.Modified;
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region 审核或取消审核
        /// <summary>
        /// 审核或取消审核
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int PassOrNo(int id)
        {
            using (Entities db = new Entities())
            {
                var q = db.DeliveryMethods.Where(x => x.Id == id).First();
                q.Pass = 1 - q.Pass;

                db.SaveChangesAsync().Wait();
                return q.Pass;
            }
        }

        #endregion

        #region  删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="Ids"></param>
        public void Delete(int[] Ids)
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.DeliveryMethods where Ids.Contains(x.Id) select x;
                q.ToList().ForEach(x => db.DeliveryMethods.Remove(x));

                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region  加载数据
       /// <summary>
        /// 加载数据
       /// </summary>
       /// <param name="Id"></param>
       /// <returns></returns>
        public DeliveryMethod GetInfo(int Id)
        {
            using (Entities db = new Entities())
            {
                return db.DeliveryMethods.Find(Id);
            }
        }
        #endregion

        #region  加载数据
       /// <summary>
        /// 加载数据
       /// </summary>
       /// <returns></returns>
        public IList<DeliveryMethod> BindList()
        {
            using (Entities db = new Entities())
            {
                return db.DeliveryMethods.Where(x => x.Pass == 1).ToList();
            }
        }
        #endregion

        #region 绑定分页数据
        /// <summary>
        /// 绑定分页数据
        /// </summary>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<DeliveryMethod> BindList(PagingParam pp)
        {
            using (Entities db = new Entities())
            {
                var query = from x in db.DeliveryMethods
                            select x;
                pp.RecordCount = query.Count();

                return query.OrderByDescending(x => x.Id).Skip(pp.Skip).Take(pp.PageSize).ToList();
            }
        }
        
        #endregion
    }
}